/*******************************************************************************
* file  : md5 demo
* creat : apleilx
* data  : 2017/03/02
* @brief  用于生成数据流摘要，验证数据流完整性，结果为16字节
*		堆栈无要求
*******************************************************************************/
#include "bsp.h"
#include "apm32f4xx_hash.h"
#include "framework.h"
#include "crc_ext.h"
#include "mbedtls/md5.h"

/*******************************************************************************
* @brief  md5_demo.
* \param[in] none
* \retval: none
*******************************************************************************/
void md5_demo(void)
{
	uu32 clk_start;
	void * src , *out;
	
	RCM_EnableAHB2PeriphClock(RCM_AHB2_PERIPH_HASH);
	
	dbg_print("\nmbed MD5 test :\n");
	
	// 请求内存
	src = malloc(2000);
	out = malloc(64);
	
	// 输入数据流，长度任意
	memcpy(src, &sinx2_0r5pi_2048_16384[32], 1200);	
	
	dbg_puts(SYMBOL_LINE_EQ PTCR);
	
	// 开始
	clk_start = bsp_cpu_clks_get();
	// mbedtls_md5 = init + start + update + finish + free
	mbedtls_md5(src, 1024, out);
	dbg_print("mbedtls MD5 time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
	dbg_u8_print("MD5 OUT: ", out, 16, 20);
	
	dbg_puts(SYMBOL_LINE_ADD PTCR);
	clk_start = bsp_cpu_clks_get();
	HASH_ComputeMD5((uint8_t*)src, 1024, out);
	dbg_print("APM32 MD5 time: %d us\n", bsp_clks_to_us(bsp_cpu_clks_get() - clk_start));
	dbg_u8_print("MD5 OUT: ", out, 16, 20);
	
	// 释放内存
	free(src);
	free(out);
}

/*******************************************************************************
 * @brief: md5_cal
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int md5_cal(int argc, char *argv[])
{
	md5_demo();
	
	return 0;
}
DBG_FUN_EXPORT(md5_cal);
